* Java bean support code

Common code for Java beans, used in my other projects. Having this code in a library cuts down on boilerplate.

** Status of the project

[[https://github.com/steinarb/beans/actions/workflows/beans-maven-ci-build.yml][file:https://github.com/steinarb/beans/actions/workflows/beans-maven-ci-build.yml/badge.svg]]
[[https://coveralls.io/github/steinarb/beans][file:https://coveralls.io/repos/github/steinarb/beans/badge.svg]]
[[https://sonarcloud.io/summary/new_code?id=steinarb_beans][file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_beans&metric=alert_status#.svg]]
[[https://maven-badges.herokuapp.com/maven-central/no.priv.bang.beans/beans][file:https://maven-badges.herokuapp.com/maven-central/no.priv.bang.beans/beans/badge.svg]]
[[https://www.javadoc.io/doc/no.priv.bang.beans/beans][file:https://www.javadoc.io/badge/no.priv.bang.beans/beans.svg]]

[[https://sonarcloud.io/summary/new_code?id=steinarb_beans][file:https://sonarcloud.io/images/project_badges/sonarcloud-white.svg]]

[[https://sonarcloud.io/summary/new_code?id=steinarb_beans][file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_beans&metric=sqale_index#.svg]]
[[https://sonarcloud.io/summary/new_code?id=steinarb_beans][file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_beans&metric=coverage#.svg]]
[[https://sonarcloud.io/summary/new_code?id=steinarb_beans][file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_beans&metric=ncloc#.svg]]
[[https://sonarcloud.io/summary/new_code?id=steinarb_beans][file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_beans&metric=code_smells#.svg]]
[[https://sonarcloud.io/summary/new_code?id=steinarb_beans][file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_beans&metric=sqale_rating#.svg]]
[[https://sonarcloud.io/summary/new_code?id=steinarb_beans][file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_beans&metric=security_rating#.svg]]
[[https://sonarcloud.io/summary/new_code?id=steinarb_beans][file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_beans&metric=bugs#.svg]]
[[https://sonarcloud.io/summary/new_code?id=steinarb_beans][file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_beans&metric=vulnerabilities#.svg]]
[[https://sonarcloud.io/summary/new_code?id=steinarb_beans][file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_beans&metric=duplicated_lines_density#.svg]]
[[https://sonarcloud.io/summary/new_code?id=steinarb_beans][file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_beans&metric=reliability_rating#.svg]]

** Release history

| Date                   | Version | Comment                                                             |
|------------------------+---------+---------------------------------------------------------------------|
| <2022-05-29 Sun 08:26> |   1.2.0 | Use karaf 4.4.0 and OSGi 8                                          |
| <2021-06-09 Wed 21:46> |   1.1.4 | Avoid dependencyManagement version "leakage" in the BoM             |
| <2021-04-18 Sun 22:47> |   1.1.2 | Add a "Bill of Materials" (BoM)                                     |
| <2021-04-15 Thu 00:43> |   1.1.1 | Get common maven dependencies and maven plugin config from parent   |
| <2021-04-11 Sun 21:43> |   1.1.0 | Built with karaf 4.3.0 and OSGi 7                                   |
| <2019-12-31 Tue 00:47> |   1.0.0 | Adds bean base class Immutable implementing hashCode() and equals() |

** Overview of the project

*** Immutable

This is an OSGi bundle that contains the class Immutable which serves as a base class for immutable Java beans and provides the beans with implementations of hashCode() and equals().

The Object.hashCode() and Object.equals() methods must be overriden from their default Object implmentations in data objects, if those data objects are to work properly in maps and sets. However, writing hashCode() and equals() methods manually is boring. It consists of creating a lot of boilerplate code, and that boilerplate needs to have tests written if I don't wish to have sonar complain about test coverage, increasing the code that has to be modified when the bean changes.

IDEs like eclipse and IntelliJ can create hashCode() and equals() methods, but the auto-generated code may need a little tinkering, and the tests needs to be handwritten, or your code coverage will become low enough for sonar to complain. And if the beans change the IDE can recreate the methods, but the tests still needs to be changed manually

The apache [[https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder/HashCodeBuilder.html][commons-lang3 HashCodeBuilder]] has methods to create hashCode() using reflection, but using reflections means there is a performance cost. However, since the beans used in my jackson/jersey REST APIs are invariably immutable, it is possible to create the hashCode() lazily and then cache it.  And that's what the Immutable bean base class found in this library does.

The equals() method also depends on the hashCode(), so it may not be entirely comparison proof, but for the first version I'm trying it out.

To use the latest version of the OSGi bundle in a maven project, or just use it as a plain JAR file, add the following dependencies (commons-lang3 is a provided dependency in the beans.immutable pom, so won't become a transitive dependency and must be added explicitly to a project using beans.immutable, even if the project is just using beans.immutable as a plain JAR file):
#+begin_src xml
  <dependencies>
      <dependency>
          <groupId>no.priv.bang.beans</groupId>
          <artifactId>beans.immutable</artifactId>
          <version>1.2.0</version>
      </dependency>
      <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-lang3</artifactId>
          <version>3.9</version>
      </dependency>
  </dependencies>
#+end_src

To use the latest version of the OSGi bundle in a maven project, building an OSGi bundle intended for use in [[https://karaf.apache.org][apache karaf]]:
 1. Add a provided dependency to beans.immutable (to make things compile) and a test dependency to commons-lang3 (to prevent tests from failing in startup. If you already have a dependency to commons-lang3 you don't need this test dependency):
    #+begin_src xml
      <dependencies>
          <dependency>
              <groupId>no.priv.bang.beans</groupId>
              <artifactId>beans.immutable</artifactId>
              <version>1.2.0</version>
              <scope>provided</scope>
          </dependency>
          <dependency>
              <groupId>org.apache.commons</groupId>
              <artifactId>commons-lang3</artifactId>
              <version>3.9</version>
              <scope>test</scope>
          </dependency>
      </dependencies>
    #+end_src
 2. Add a load of the feature repository containing beans.immutable and depend on the beans-immutable feature from your projects template feature.xml (src/main/feature/feature.xml):
    #+begin_src xml
      <features>
          <repository>mvn:no.priv.bang.beans/beans/1.2.0/xml/features</repository>
          <feature name="my-feature">
              <feature>beans-immutable</feature>
          </feature>
      </features>
    #+end_src

** License

This code is licensed under the Apache license v. 2.  See the LICENSE file for details.
